/*
 * generate ctd file, by edhong
 * Jan-March 2008
 *
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#include "contexttree.h"

#define USG_STRING "[-b %d:numbitplanes=10] [-nl %d:waveletlevels=(enough to make LL have side of length <=8)] [-ct %d:characteristictype=1] [-d %d:debugflag=0] [-i %s:imgfile] %s:ctdfileename"

QccString imgfilename = "";
QccString ctdfilename;

int NumLevels = -1;
int NumSubbands;
int NumBitplanes = 10;
int CharacteristicType = 1;
int debugflag = 0;

QccIMGImage InputImage;
QccWAVSubbandPyramid SubbandPyramid;

int ImageNumCols;
int ImageNumRows;


int main(int argc, char *argv[])
{
  
  QccInit(argc, argv);

  QccIMGImageInitialize(&InputImage);
  
  if (QccParseParameters(argc, argv,
                         USG_STRING,
                         &NumBitplanes,
                         &NumLevels,
			 &CharacteristicType, 
			 &debugflag,
                         InputImage.filename,
                         &ctdfilename))
    QccErrorExit();

  if (NumBitplanes < 1)
    {
      QccErrorAddMessage("%s: Number of bitplanes must be 1 or greater",
                         argv[0]);
      QccErrorExit();
    }
     
  unsigned long long flags[2];
  

  if (InputImage.filename[0] != '\0') {
    if (QccIMGImageRead(&InputImage))
      {
	QccErrorAddMessage("%s: Error calling QccIMGImageRead()",
			   argv[0]);
	QccErrorExit();
      }
  
    ImageNumCols = InputImage.Y.num_cols;
    ImageNumRows = InputImage.Y.num_rows;


    NumLevels = computeNumWaveletLevels(ImageNumRows, ImageNumCols);
  }

  if (NumLevels < -1)
    {
      QccErrorAddMessage("%s: Number of levels must be non-negative",
                         argv[0]);
      QccErrorExit();
    }
  else if (NumLevels == -1) {
      QccErrorAddMessage("%s: Must specify number of levels or image size",
                         argv[0]);
      QccErrorExit();
  }

  NumSubbands = QccWAVSubbandPyramidNumLevelsToNumSubbands(NumLevels);

  InitAllC(NumSubbands, NumBitplanes);

  if (getCharacteristicSet(CharacteristicType, flags ))
    {
      QccErrorExit();
    }
 

  printf("writing to %s \n",  ctdfilename);
  printf("  parameters: %d bit-planes, %d wavelet levels, Characteristic type=%d, debug=%d\n",
	 NumBitplanes, NumLevels, CharacteristicType, debugflag);

  contexttree ct;
  if (create_imgsplitcontexttree(flags, &ct)) {
    printf("%s: error calling create_imgsplitcontexttree\n", argv[0]);
  }

  if (debugflag==1) printcontexttree(&ct);
  
  printctdfile(ctdfilename, &ct);

  QccExit;
}
